package com.example.gustavo.hello.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by gustavo on 2015/5/5.
 */
public class SideBar extends View {
    private String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "#" };
    private Paint paint = new Paint();
    private int singleHeight;
    private int choose = -1;//选中位置

    public SideBar(Context context) {
        super(context);
        init();
    }

    public SideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public SideBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    private void init(){
        System.err.println("init");



    }


    @Override
    protected void onFinishInflate() {
        System.err.println("onFinishInflate");
        super.onFinishInflate();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        System.err.println("onMeasure");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        System.err.println("onSizeChanged");

        singleHeight = getHeight() / letters.length;

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        System.err.println("onLayout");
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        System.err.println("onDraw");
        super.onDraw(canvas);



        for (int i = 0; i < letters.length; i++) {

            paint.setColor(Color.RED);
            paint.setAntiAlias(true);
            paint.setTextSize(20);
            float x = getWidth()/2 - paint.measureText(letters[i])/2;

            if(choose == i){
                paint.setColor(Color.YELLOW);
            }

            canvas.drawText(letters[i], x, singleHeight * i + singleHeight, paint);
        }


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){

            case MotionEvent.ACTION_UP:

                choose = -1;

                invalidate();

                break;

            default:

                float i = event.getY() / getHeight();

                System.out.println("i = " + i);
                int index = (int) (i * letters.length);
                choose = index;

                System.out.println("index = " + index);
                String letter = letters[index];
                System.out.println("letter = " + letter);

                invalidate();
                break;

        }


        return true;
    }
}
